import  pygame
from Config import FONT
from My_functions import draw_filled_equilateral_triangle, BLACK
from Skins import player_skin_1


class Button:
    def __init__(self, x, y, width, height, text, color):
        self.rect = pygame.Rect(x, y, width, height)
        self.text = text
        self.press_down = False
        self.background_color = color

    def draw(self, screen):
        if self.press_down:
            pygame.draw.rect(screen, self.background_color, self.rect)
            font = pygame.font.Font(FONT, 16)
            text_surface = font.render(self.text, True, (255,255,255))
            text_rect = text_surface.get_rect(center=self.rect.center)
            screen.blit(text_surface, text_rect)
        else :
            pygame.draw.rect(screen, self.background_color, self.rect)
            font = pygame.font.Font(FONT, 16)
            text_surface = font.render(self.text, True, (0, 0, 0, 0))
            text_rect = text_surface.get_rect(center=self.rect.center)
            screen.blit(text_surface, text_rect)
        self.press_down = False

    def is_clicked(self, pos):
        if  self.rect.collidepoint(pos):
            self.press_down = True
            return True
        return False

class SwitchList:
    def __init__(self, x, y, width, height, list,name):
        self.image = pygame.Surface((width, height), pygame.SRCALPHA)
        self.list = list
        self.name = name
        self.now = 0
        self.rect = self.image.get_rect()
        self.rect.x = x
        self.rect.y = y
        self.left_button_rect = pygame.Rect(90,height//2-5, 10, 10)
        self.right_button_rect = pygame.Rect(width-55,height//2-5, 10, 10)

    def draw(self, screen):
        print(self.now)
        # 在 self.image 上绘制背景色
        pygame.draw.rect(self.image, (155, 155, 90), self.image.get_rect())

        # 绘制文本
        font = pygame.font.Font(FONT, 16)
        text_surface = font.render(self.list[self.now], True, BLACK)
        text_rect = text_surface.get_rect(x=self.rect.x+self.rect.width//2+text_surface.get_height()//2,y=self.rect.y+text_surface.get_height()//4)

        #
        text_surface2 = font.render(self.name, True, BLACK)
        text_rect2 = text_surface.get_rect(x=self.rect.x,y=self.rect.y+text_surface2.get_height()//4)

        # 绘制箭头
        draw_filled_equilateral_triangle(self.image, self.left_button_rect, BLACK, 'left')
        draw_filled_equilateral_triangle(self.image, self.right_button_rect, BLACK, 'right')

        # 将 self.image 绘制到屏幕上
        screen.blit(self.image, self.rect)

        # 将文本直接绘制到屏幕上
        screen.blit(text_surface, text_rect)
        screen.blit(text_surface2, text_rect2)

    def is_clicked(self, pos):
        # 将鼠标点击位置转换为相对于 self.rect 的位置
        relative_pos = (pos[0] - self.rect.x, pos[1] - self.rect.y)

        if self.left_button_rect.collidepoint(relative_pos):
            self.now = (self.now - 1 + len(self.list)) % len(self.list)

        if self.right_button_rect.collidepoint(relative_pos):
            self.now = (self.now + 1 + len(self.list)) % len(self.list)






